"""
@author：yiwang
@file_name: __init__.py
@create date: 2024-01-05
@file_description：
"""
from apps.NGS_API.common.common_time import get_current_time
from apps.NGS_API.connector.db_common import *
from flask_login import current_user

class sample_stock_log(object):
    def __init__(self,form):
        self.form = form
        self.action_type = {
            "-2":"清退",
            "1":"入库",
            "-1":"出库",
            "0":"删除"
        }
        
    def sample_delete_log(self,infos:dict):
        table_cols = ""
        table_values = '''
            \"{action_id}\",
            \"{sample_id}\",
            \"{operate_user}\",
            \"{check_user}\",
            {action_type},
            \"{operate_time}\",
            \"{create_time}\",
            \"{modify_time}\"
            '''.format(
                action_id="ACT90001",
                sample_id=infos["sample_id"],
                operate_user=current_user.username,
                check_user=current_user.username,
                action_type=0,
                operate_time=get_current_time(),
                create_time=get_current_time(),
                modify_time=get_current_time()
        )
        for _,values in self.form["match"].items():
            table_cols = table_cols + values.split("|")[0] + ","
        table_cols = table_cols.rstrip(",")
        insert_log_sql = '''
            INSERT INTO {table_name} 
                ({table_cols})
            VALUES 
                ({table_values})
        '''.format(
            table_name = self.form["name"],
            table_cols = table_cols,
            table_values = table_values
        )
        add_info(insert_log_sql)
    
    def sample_outstock_log(self,infos:dict):
        table_cols = ""
        table_values = '''
            \"{action_id}\",
            \"{sample_id}\",
            \"{operate_user}\",
            \"{check_user}\",
            {action_type},
            \"{operate_time}\",
            \"{create_time}\",
            \"{modify_time}\"
            '''.format(
                action_id="ACT90001",
                sample_id=infos["sample_id"],
                operate_user=current_user.username,
                check_user=current_user.username,
                action_type=-1,
                operate_time=get_current_time(),
                create_time=get_current_time(),
                modify_time=get_current_time()
        )
        for _,values in self.form["match"].items():
            table_cols = table_cols + values.split("|")[0] + ","
        table_cols = table_cols.rstrip(",")
        insert_log_sql = '''
            INSERT INTO {table_name} 
                ({table_cols})
            VALUES 
                ({table_values})
        '''.format(
            table_name = self.form["name"],
            table_cols = table_cols,
            table_values = table_values
        )
        add_info(insert_log_sql)